
### Coefficient plots ###

b <- list(geom_vline(xintercept = 0, color = 'black'))

coefficientsnames_plot <- c("Stigma" = "Stigma",
                       "Newfriends" = "New friends\n since joining",
                       "Partyrecruitment" = "Recruited by\n the party",
                       "Ambitioncand" = "Electoral ambition",
                       "Policyinfluence" = "Political efficacy",
                       "Gender" = "Gender (Woman)")

plot1 <- modelplot(model_11,
                   vcov = ~Party,
                     size = 0.3,
                     coef_map = coefficientsnames_plot,
                     background = b,
                     conf_level = 0.95,
                     draw = FALSE)

p1 <- ggplot(plot1, aes(x = estimate, y = term)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +  # Add vertical line at zero
  geom_point(color = "black", size = 2) +  # Adjust point color and size
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), color = "black", height = 0.15) +
  labs(title = "Online",
       x = "",
       y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 15))

plot2 <- modelplot(model_5,
                   vcov = ~Party,
                   size = 0.3,
                   coef_map = coefficientsnames_plot,
                   background = b,
                   conf_level = 0.95,
                   draw = FALSE)

p2 <- ggplot(plot2, aes(x = estimate, y = term)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +  # Add vertical line at zero
  geom_point(color = "black", size = 2) +  # Adjust point color and size
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), color = "black", height = 0.15) +
  labs(title = "Attending party meetings",
       x = "",
       y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 15))

plot3 <- modelplot(model_17,
                   vcov = ~Party,
                   size = 0.3,
                   coef_map = coefficientsnames_plot,
                   background = b,
                   conf_level = 0.95,
                   draw = FALSE)

p3 <- ggplot(plot3, aes(x = estimate, y = term)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +  # Add vertical line at zero
  geom_point(color = "black", size = 2) +  # Adjust point color and size
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), color = "black", height = 0.15) +
  labs(title = "Distributing flyers",
       x = "",
       y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 15))

plot4 <- modelplot(model_23,
                   vcov = ~Party,
                   size = 0.3,
                   coef_map = coefficientsnames_plot,
                   background = b,
                   conf_level = 0.95,
                   draw = FALSE)

p4 <- ggplot(plot4, aes(x = estimate, y = term)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +  # Add vertical line at zero
  geom_point(color = "black", size = 2) +  # Adjust point color and size
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), color = "black", height = 0.15) +
  labs(title = "Intra-party office",
       x = "",
       y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 15))

plot5 <- modelplot(model_29,
                   vcov = ~Party,
                   size = 0.3,
                   coef_map = coefficientsnames_plot,
                   background = b,
                   conf_level = 0.95,
                   draw = FALSE)

p5 <- ggplot(plot5, aes(x = estimate, y = term)) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +  # Add vertical line at zero
  geom_point(color = "black", size = 2) +  # Adjust point color and size
  geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), color = "black", height = 0.15) +
  labs(title = "Candidature",
       x = "",
       y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5, size = 15))

grid.arrange(p1, p2, p3, p4, p5, ncol = 2, nrow = 3)

### Causal mediation analysis ###

med_results1 <- summary(PCA_recruit_results)

results_df1 <- data.frame(
  Effect = c("ACME", "ADE", "Total Effect"),
  Estimate = c(med_results1$d.avg, med_results1$z.avg, med_results1$tau.coef),
  Lower = c(med_results1$d.avg.ci[1], med_results1$z.avg.ci[1], med_results1$tau.ci[1]),
  Upper = c(med_results1$d.avg.ci[2], med_results1$z.avg.ci[2], med_results1$tau.ci[2])
)

p6 <- ggplot(results_df1, aes(x = Estimate, y = Effect)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = Lower, xmax = Upper), height = 0.15) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  labs(title = "Mediator: Recruited by the party", x = "Effect Estimate", y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        axis.title = element_text(size = 12),
        plot.title = element_text(hjust = 0.5, size = 15))

med_results2 <- summary(PCA_friends_results)

results_df2 <- data.frame(
  Effect = c("ACME", "ADE", "Total Effect"),
  Estimate = c(med_results2$d.avg, med_results2$z.avg, med_results2$tau.coef),
  Lower = c(med_results2$d.avg.ci[1], med_results2$z.avg.ci[1], med_results2$tau.ci[1]),
  Upper = c(med_results2$d.avg.ci[2], med_results2$z.avg.ci[2], med_results2$tau.ci[2])
)

p7 <- ggplot(results_df2, aes(x = Estimate, y = Effect)) +
  geom_point(size = 2) +
  geom_errorbarh(aes(xmin = Lower, xmax = Upper), height = 0.15) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  labs(title = "Mediator: New friends since joining", x = "Effect Estimate", y = "") +
  theme_light() +
  theme(axis.text = element_text(size = 10),
        axis.title = element_text(size = 12),
        plot.title = element_text(hjust = 0.5, size = 15))

grid.arrange(p6, p7, ncol = 2, nrow = 1)



